/*
 * @Descripttion: 
 * @Author: liys
 * @LastEditTime: 2021-07-05 16:59:58
 * @Email: 21555@sohu.com
 */

$(function(){
	var type = $("#type").val();
	var url = "surroudingJson?type=" + type;
		/*初始化表格*/
		$('#dg').datagrid({
    		url:url,
    		collapsible:true,
    		fit:true,
			fitColumns:true,
			toolbar:'#footer',
			pagination:true,
			pageSize:20,
			pageList:[20,50,100,200],
			remoteSort:false,
			onLoadSuccess:function(data){
		        if (data.total == 0) { 
				$(this).datagrid('appendRow', { lineName: '<div style="text-align:center;color:red">没有相关记录！</div>' }).datagrid('mergeCells', { index: 0, field: 'lineName', colspan: 4 }) 
				$(this).closest('div.datagrid-wrap').find('div.datagrid-pager').hide(); 
				 $(this).datagrid('hideColumn', 'ck');
				} 
				else {
					$(this).closest('div.datagrid-wrap').find('div.datagrid-pager').show();
					$(this).datagrid('showColumn', 'ck');}
		    },
   			columns:[[
   			 	{field:'ck',checkbox:true},
   			 	{field:'lineID',hidden:'true'},
				{field:'lineName',title:'线路名称',align:'center',width:'25%',
				 formatter:function(value,row,index){
					if(row.lineID == undefined)return row.lineName;
					return'<a href="linebaseinfo?LineID='+row.lineID+'">'+row.lineName+'</a>'
				}},
				{field:'day',title:'旅游天数',align:'center',width:'23%'},
				{field:'zoneName',title:'所属区域',align:'center',width:'25%'},
				{field:'creator',title:'创建人',align:'center',width:'25%'}
    		]]
		});
		
		var url2 = "surroudingName?type=" + type;
		$("#LineName").combobox({
			url:url2,
			valueField:'iLineID',
       		textField:'cLineName',
		})
		
		/*省市区*/
	var data = [{'iZoneID' : '', 'cZoneName' : '不限'}];
    $("#provinceTest").combobox({
        url:'getProvince',
        valueField:'iZoneID',
        textField:'cZoneName',
		editable:false,
		data:data,
        filter: function(q, row){
            var opts = $(this).combobox('options');
            return row[opts.textField].indexOf(q) >= 0;
        },
        onSelect:function(rec){
			var zoneID = rec.iZoneID;
			if(zoneID == "")return;
            $('#cityTest').combobox({
                url:'getCity?zoneID='+zoneID,
                valueField:'iZoneID',
                textField:'cZoneName',
                filter: function(q, row){
                    var opts = $(this).combobox('options');
                    return row[opts.textField].indexOf(q) >= 0;
                },
            });
        }
    });
	
    $("#cityTest").combobox({
        valueField:'iZoneID',
        textField:'cZoneName',
		editable:false,
		data:data,
        filter: function(q,row){
            var opts = $(this).combobox('options');
            return row[opts.textField].indexOf(q) >= 0;
        },
        onSelect:function(rec){
			var zoneID = rec.iZoneID;
			if(zoneID == "")return;
            $('#districtTest').combobox({
                url:'getArea?zoneID='+zoneID,
                valueField:'iZoneID',
                textField:'cZoneName',
                filter: function(q, row){
                    var opts = $(this).combobox('options');
                    return row[opts.textField].indexOf(q) >= 0;
                },
            });
        }
    });

    $("#districtTest").combobox({

        valueField:'iZoneID',
        textField:'cZoneName',
		editable:false,
		data:data,
        filter: function(q, row){
            var opts = $(this).combobox('options');
            return row[opts.textField].indexOf(q) >= 0;
        },
    });		
			
});

/*搜索*/
function search(){
	
		var lineName = $("#LineName").combobox('getText');
		var province = $("#provinceTest").combobox('getValue');
		var city = $("#cityTest").combobox('getValue');
		var zoneID = $("#districtTest").combobox('getValue');
		var type = $("#type").val();
		$("#dg").datagrid("load",{
			lineName:lineName,
			privince:province,
			city:city,
			zoneID:zoneID,
			type:type
		})
	}
	
/*清空*/
function clearSurroudIndex(){
        $("#LineName").combobox('clear');
		$('#provinceTest').combobox('setValue', '');
		$('#cityTest').combobox('setValue', '');
		$('#districtTest').combobox('setValue', '');
     }
/*新建*/
function create(){
	var type = $("#type").val();
	var url = "/mmy/tocreatesurrouding?type=" + type;
	location.href= url;
}
/*修改*/
function toUpdateLineBaseTrip(){
		var rows = $('#dg').datagrid('getSelections');
		var checkNum = rows.length;
		if(checkNum==0){
			$.messager.alert('错误','至少选择一项','error');
		 } else if(checkNum>1){
			$.messager.alert('错误','最多选择一项','error');
		 }else{
			var iLineID = rows[0].lineID;
			location.href="/mmy/toupdatelinebasetrip?iLineID="+iLineID;
		 }
} 

/*复制*/
function toCopyLineBaseTrip(){
		var type = $("#type").val();
		var rows = $('#dg').datagrid('getSelections');
		var checkNum = rows.length;
		if(checkNum==0){
			$.messager.alert('错误','至少选择一项','error');
		 } else if(checkNum>1){
			$.messager.alert('错误','最多选择一项','error');
		 }else{
			var iLineID = rows[0].lineID;
			var url = "/mmy/tocopylinebasetrip?iLineID=" + iLineID + "&type=" + type;
			location.href = url;
		 }
	}	
/* 删除*/
function delSurroudingBatch(){
	var rows = $('#dg').datagrid('getSelections');
		var checkNum = rows.length;
		if(checkNum==0){
			$.messager.alert('错误','至少选择一项','error');
		 }else{
			var checklist = new Array();
			var checkName = [];
			checkName.push('将要删除以下线路');
			for(var i=0; i<rows.length; i++){
				
				checklist.push(rows[i].lineID);
				checkName.push('<span>'+rows[i].lineName +'</span>');
			}
			$.messager.confirm('提示',checkName, function(r){
    		if (r){
				$.ajax({
			        url:"/mmy/deletesurroudingchecked",
			        type:"post",
			        data:{
			            checkList:checklist.toString()
			        },
			        dataType:"text",
			       success:function () {
						 $("#dg").datagrid("reload");
			             $.messager.show({title:'消息',msg:'删除成功',timeout:3000,});
					},
			        error:function () {
			           $.messager.show({title:'消息',msg:'删除失败',timeout:3000,});
			        }
			    })
    		}
		});		
	}
}

/*计划*/
function surroudingplan(){
	var rows = $('#dg').datagrid('getSelections');
		var checkNum = rows.length;
		if(checkNum==0){
			$.messager.alert('错误','至少选择一项','error');
		 } else if(checkNum>1){
			$.messager.alert('错误','最多选择一项','error');
		 }else{
			var iLineID = rows[0].lineID;
			location.href="/mmy/surroudingplan?iLineID="+iLineID;
		 }
}

/*出团*/
function group(){
	
	location.href="/mmy/linegroup";
		 
}

/*单据*/
function order(){
	
	var type = $("#type").val();
	var url = "/mmy/orderreserved?type=" + type;
	location.href = url;
}




